import { Component, Label, Node, Prefab, UITransform, Widget, _decorator, instantiate } from 'cc';
const { ccclass, property } = _decorator;

@ccclass("UIMsg")
export class UIMsg extends Component {

    @property(Prefab)
    labMsg: Prefab;
    @property(Node)
    msgs: Node;

    addMsg(msg: kp_com_msg) {
        let lab: Label;
        if (this.msgs.children.length < 10) {
            const node = instantiate(this.labMsg);
            lab = node.getComponent(Label);
        } else {
            lab = this.msgs.children[0].getComponent(Label);
        }
        lab.string = msg.msg;
        lab.node.removeFromParent();
        this.msgs.addChild(lab.node);

        const trans = this.msgs.getComponent(UITransform);
        const widget = this.msgs.getComponent(Widget);
        if (trans.height > this.msgs.parent.getComponent(UITransform).height) {
            widget.isAlignTop = false;
            widget.isAlignBottom = true;
            widget.bottom = 0;
            trans.anchorY = 0;
        } else {
            widget.isAlignTop = true;
            widget.isAlignBottom = false;
            widget.top = 0;
            trans.anchorY = 1;
        }
    }

}